package com.example.main;

import com.example.bean.Fruit;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class BuyFruit {
    public static void main(String[] args) {
        //苹果
        Fruit apple=new Fruit();
        apple.setFruitName("苹果");
        apple.setPrice(new BigDecimal(8));
        apple.setWeight(2);
        apple.setCountPrice();
        //草莓
        Fruit strawberry=new Fruit();
        strawberry.setFruitName("草莓");
        strawberry.setPrice(new BigDecimal(13));
        strawberry.setWeight(2);
        strawberry.setCountPrice();
        //芒果
        Fruit mango=new Fruit();
        mango.setFruitName("芒果");
        mango.setPrice(new BigDecimal(13));
        mango.setWeight(2);
        mango.setCountPrice();
        List<Fruit> list=new ArrayList<>();
        list.add(apple);
        list.add(strawberry);
        list.add(mango);
        BigDecimal countPrice=countPrice(list);
        System.out.println(countPrice);
    }
    /**
     * 计算购买水果的总价钱
     */
    public static BigDecimal countPrice(List<Fruit> list){
        BigDecimal count=new BigDecimal(0);
        for (Fruit fruit:list){
            //计算水果总价钱
            count=count.add(fruit.getCountPrice());
        }
        //如果价钱超过100，减10元
        if (count.subtract(new BigDecimal(100)).intValue()>0){
            count=count.subtract(new BigDecimal(10));
        }
        return count;
    }
}
